package com.mengmablog.blog.service.impl;

import com.mengmablog.blog.commons.ResponseCode;
import com.mengmablog.blog.commons.ServerResponse;
import com.mengmablog.blog.dao.UsersMapper;
import com.mengmablog.blog.entity.Users;
import com.mengmablog.blog.service.port.IUserService;
import com.mengmablog.blog.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public ServerResponse login(String username, String password) {
        //通过用户名与密码查询数据库进行判断是否正确 ————提示用户名或密码错误
        String md5PassWord = MD5Util.MD5EncodeUtf8(password);
        Users currentUser = usersMapper.loginByUserNameAndPd(username, md5PassWord);
        if (currentUser == null) {
            return ServerResponse.createByError();
        }
        return ServerResponse.createBySuccess(currentUser);
    }

    @Override
    public ServerResponse reset(String username, String oldPassword, String newPassword) {
        int i = usersMapper.updateByReset(username, oldPassword, newPassword);
        if (i > 0) {
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("用户名与登陆的不符合");
    }
}
